<?php

/*
 *
 *
 */
define('ABSPATH', dirname(__FILE__) . '/');
if (function_exists('xdebug_start_trace')) {
    //xdebug_start_trace(ABSPATH . 'trace.xt' . uniqid());
}
ob_implicit_flush(true);
require_once(dirname(__FILE__) . '/../include.all.php');

$time = time();

$def = new Deferred();
$def->addBoth(function($res) {
	$def = new Deferred();
	setTimeout(function() use($def, $res) {
		$def->callback($res . 'b');
	}, 1);
	var_dump('time1: ' . (time() - $GLOBALS['time']));
	return $def;
});
$def->addBoth(function($res) {
	$def = new Deferred();
	setTimeout(function() use($def, $res) {
		$def->callback($res . 'c');
	}, 1);
	var_dump('time2: ' . (time() - $GLOBALS['time']));
	return $def;
});
$def->addBoth(function($res) {
	$def = new Deferred();
	$def->callback($res . 'd');
	var_dump('time3: ' . (time() - $GLOBALS['time']));
	return $def;
});
$def->addBoth(function($res) {
	return $res . 'e';
});
$def->addBoth(function($res) {
	$def = new Deferred();
	setTimeout(function() use($def, $res) {
		$def->callback($res . 'f');
	}, 1);
	var_dump('time4: ' . (time() - $GLOBALS['time']));
	return $def;
});
$def->addBoth(function($res) {
	var_dump('time5: ' . (time() - $GLOBALS['time']));
	var_dump($res);
	$def = new Deferred();
	$def->callback($res);
	return $def;
});


$def->callback('a');

echo "\nAll Init\n";
Tick::getInstance()->startExecution();
echo "\nAll Ended\n";